package com.test003;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.test001.RabbitMQConnection;


/**
 * @ClassName Producer  事务的消息确认
 * @Version V1.0
 **/
public class Producer {
    private static final String QUEUE_NAME = "mayikt-queue1";

    public static void main(String[] args) throws  Exception {
        //1.创建一个新连接
        Connection connection = RabbitMQConnection.getConnection();
        //2.设置channel
        Channel channel = connection.createChannel();
        try {
            //3.发送消息
            String msg = "事务消息";
            channel.txSelect();
            channel.basicPublish("", QUEUE_NAME, null, msg.getBytes());
            // int i=1/0;
            channel.txCommit(); 
            System.out.println("消息投递成功");
            channel.close();
            connection.close();
        } catch (Exception e) {
        	System.out.println("异常");
        	 channel.txRollback();
        }
    }
}
